package com.song.user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.song.common.model.PageModel;
import com.song.common.model.UserModel;
import com.song.common.util.PageUtil;
import com.song.user.entity.UserInfo;
import com.song.user.entity.UserRole;
import com.song.user.mapper.UserInfoMapper;
import com.song.user.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.song.user.service.UserRoleService;
import com.song.user.view.po.UserPO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author song
 * @since 2024-06-05
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    private UserRoleService userRoleService;

    @Override
    public PageModel<UserModel> pageList(int pageNum, int pageSize, UserInfo userInfo) {
        Page<UserModel> page = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(() -> baseMapper.queryList(userInfo));
        return PageUtil.toPageModel(page);
    }

    @Override
    public UserModel detailById(String id) {
        return baseMapper.detailById(id);
    }

    @Override
    public void deleteUserInfoById(String id) {
        //删除用户信息
        baseMapper.deleteById(id);
        //删除用户角色关联信息
        userRoleService.remove(new QueryWrapper<UserRole>().lambda().eq(UserRole::getUserId, id));
    }

    @Override
    public void addUser(UserPO userPO) {
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userPO, userInfo);
        this.save(userInfo);
        if (CollectionUtil.isNotEmpty(userPO.getRoleIds())){
            List<UserRole> userRoles = userPO.getRoleIds().stream().map(roleId -> {
                UserRole userRole = new UserRole();
                userRole.setRoleId(roleId).setUserId(userInfo.getId());
                return userRole;
            }).collect(Collectors.toList());
            userRoleService.saveBatch(userRoles);
        }
    }

    @Override
    public void updateUser(UserPO userPO) {
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userPO, userInfo);
        this.updateById(userInfo);
        //删除用户角色关联信息
        userRoleService.remove(new QueryWrapper<UserRole>().lambda().eq(UserRole::getUserId, userInfo.getId()));
        if (CollectionUtil.isNotEmpty(userPO.getRoleIds())){
            List<UserRole> userRoles = userPO.getRoleIds().stream().map(roleId -> {
                UserRole userRole = new UserRole();
                userRole.setRoleId(roleId).setUserId(userInfo.getId());
                return userRole;
            }).collect(Collectors.toList());
            userRoleService.saveBatch(userRoles);
        }
    }
}
